package cn.imu.service;

import cn.imu.entity.Block;
import com.alibaba.fastjson.JSON;
import org.apache.logging.log4j.message.Message;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.boot.autoconfigure.integration.IntegrationProperties;
import org.springframework.boot.autoconfigure.rsocket.RSocketProperties;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.BrokenBarrierException;

/*
区块链对外http服务
 */
public class HTTPService {
    private BlockService blockService;
    private P2PServer p2PServer;
    private P2PClient p2PClient;


    public HTTPService(BlockService blockService,P2PServer p2PServer,P2PClient p2PClient){
        this.blockService=blockService;
        this.p2PClient=p2PClient;
        this.p2PServer=p2PServer;
    }

    //用原生Servlet实现了接收servlet请求
    public void initHTTPServer(int port){
        try{
           //Server server=new Server(port);
            System.out.println("listening http port on:"+port);
            ServletContextHandler context=new ServletContextHandler(ServletContextHandler.SESSIONS);
            context.setContextPath("/");
            server.setHandler(context);

            //查询区块链
            context.addServlet(new ServletHolder(new ChainServlet()),"/chain");
            //创建钱包
            context.addServlet(new ServletHolder(new CreateWalletServlet()),"/wallet/create");
            //查询钱包
            context.addServlet(new ServletHolder(new GetWalletServlet()),"/wallet/get");
            //挖矿
            context.addServlet(new ServletHolder(new MineServlet()),"/mine");
            //转账交易
            context.addServlet(new ServletHolder(new NewTransactionServlet()),"/transaction/new");
            //转账交易
            context.addServlet(new ServletHolder(new GetUnpackedTransactionServlet()),"/transaction/unpacked");
            //查询钱包余额
            context.addServlet(new ServletHolder(new GetWalletBalanceServlet()),"/wallet/balance/get");

        }

    }

    private class MineServlet extends HttpServlet {

        protected void doPost(HttpServletRequest req, HttpServletResponse resp){
            resp.setCharacterEncoding("UTF-8");
            String address=req.getParameter("address");
            Wallet myWallet=blockService.getMyWalletMap().get(address);
            if(myWallet==null) {
                resp.getWriter().print("挖矿指定的钱包不存在");
                return;
            }
            Block newBlock=blockService.mine(address);
            if(newBlock==null){
                resp.getWriter().print("挖矿失败，可能有其他节点已挖出该区块");
                return;
            }
            //挖完矿之后广播出去

            Block[] blocks={newBlock};
            String msg= JSON.toJSONString(new Message(P2PService.RESPONSE_BLOCHCHAIN,JSON.toJSONString(blocks)));
            p2PServer.broatcast(msg);
            p2PClient.broatcast(msg);
            resp.getWriter().print("挖矿生成的新区块："+JSON.toJSONString(newBlock));

        }

        private classCreateWalletServlet extends HttpServlet{

            protected void doPost(HttpServletRequest req,HttpServletResponse resp){


            }
            }
        }

    }
}
